package edu.xjtlu.mobilewebservices;

import edu.xjtlu.mobilewebservices.history.HistoryList;
import edu.xjtlu.mobilewebservices.map.GPS2Map;
import edu.xjtlu.mobilewebservices.voice.VoiceRecord;
import edu.xjtlu.mobilewebservices.webservice.Barcode2WS;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FunctionList extends Activity {
	String search_type,search_code;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.functionlist);
		
		ConnectivityManager cntManager = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
		State mobile_state = cntManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		State wifi_state = cntManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		if(mobile_state == State.CONNECTED  || wifi_state == State.CONNECTED ){
			
			//notify user they are online
			Toast.makeText(this, "Network is available!", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(this, "Network is not available!", Toast.LENGTH_SHORT).show();
			
			//notify user they are off-line
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle("No Network Connected!");
			builder.setMessage("Go to Network Setting ?");
		    builder.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
				}
			});

		    builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
		    AlertDialog alert = builder.create();
		    alert.show();
		}
		
		//Spinner for searching types
		ArrayAdapter< String> adapter = new ArrayAdapter< String>(  this,  android.R.layout.simple_spinner_item);  
		adapter.setDropDownViewResource(  android.R.layout.simple_spinner_dropdown_item);  
		adapter.add("Product Name");  
		adapter.add("Shop Name");  
		adapter.add("Barcode");  
		Spinner spinner = (Spinner) findViewById(R.id.spinner);  
		spinner.setAdapter(adapter);
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
			public void onItemSelected( AdapterView<?> parent, View view, int position, long id)
			{  
			   Spinner spinner = (Spinner) parent; 
			   if(position == 0){
				   search_type = "Product Name";
			   }
			   else if(position == 1){
				   search_type = "Shop Name";
			   }
			   else {search_type = "Barcode";}
			   Log.v("Test", "id = " + id + "("  + spinner.getSelectedItem().toString() + ")"); 
			} 
			public void onNothingSelected(AdapterView<?> parent) {  }  }); 
		
		final EditText et1 = (EditText) findViewById(R.id.et1);
		et1.setOnKeyListener(new OnKeyListener(){

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if((event.getAction() == KeyEvent.ACTION_DOWN)&&
						(keyCode == KeyEvent.KEYCODE_ENTER)){
					Toast.makeText(FunctionList.this, et1.getText(), Toast.LENGTH_SHORT).show();
					search_code = et1.getText().toString();
					return true;
				}
				return false;
			}
			
		});
		/*
        et1.setOnEditorActionListener(new OnEditorActionListener(){

			@Override
			public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
			    Toast.makeText(FunctionList.this, "finish", Toast.LENGTH_SHORT).show();
				return false;
			}
        });
        */


		Button btn1 = (Button) findViewById(R.id.btn1);
		Button btn2 = (Button) findViewById(R.id.btn2);
		Button btn3 = (Button) findViewById(R.id.btn3);
		Button btn4 = (Button) findViewById(R.id.btn4);
		Button btn5 = (Button) findViewById(R.id.text_search_button);
		
		btn1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent_scan = new Intent();
				intent_scan.setClass(FunctionList.this, CaptureActivity.class);
				startActivity(intent_scan);
			}
		});
		
		btn2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent_voice = new Intent();
				intent_voice.setClass(FunctionList.this, VoiceRecord.class);
				startActivity(intent_voice);
			}
		});
		
		btn3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent_map = new Intent();
				intent_map.setClass(FunctionList.this, GPS2Map.class);
				startActivity(intent_map);
			}
		});
		
        btn4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent_history = new Intent();
				intent_history.setClass(FunctionList.this, HistoryList.class);
				startActivity(intent_history);
			}
		});
        
        btn5.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent_search = new Intent();
				Bundle bundle = new Bundle();
				if(search_code != null){
					bundle.putString("key_search_item", search_code);
					bundle.putString("key_search_type", search_type);
					intent_search.putExtras(bundle);
					intent_search.setClass(FunctionList.this, Barcode2WS.class);
					startActivity(intent_search);
				}
				else{Toast.makeText(FunctionList.this, "Pls touch Enter to confirm", Toast.LENGTH_SHORT).show();}
				
			}
		});
        
	}
}

